package core2.ch01file;


/*
 * 原理是把两个字符串的比较变成一个比较矩阵，矩阵的节点是（0（不等），1（相等））。
 * 我想请问一下，对于矩阵中查找最长的右下行线的算法是否还可以优化呢？！
 * */
public class StringMaxMatch {

	public static void main(String[] args) {
		//		System.out.println(new Date());
		//		System.out.println(getMaxMatch("abcdef", "mncdepk"));
		String[] arrays = "This is This".split(" ");
		System.out.println(arrays[0].endsWith(arrays[2]));
	}

	public static String getMaxMatch(String a, String b) {
		StringBuffer tmp = new StringBuffer();
		String maxString = "";
		int max = 0;
		int len = 0;
		char[] aArray = a.toCharArray();
		char[] bArray = b.toCharArray();
		int posA = 0;
		int posB = 0;
		while (posA < aArray.length - max) {
			posB = 0;
			while (posB < (bArray.length - max)) {
				if (aArray[posA] == bArray[posB]) {
					len = 1;
					tmp = new StringBuffer();
					tmp.append(aArray[posA]);
					while ((posA + len < aArray.length) && (posB + len < bArray.length)
							&& (aArray[posA + len] == bArray[posB + len])) {
						tmp.append(aArray[posA + len]);
						len++;
					}
					if (len > max) {
						max = len;
						maxString = tmp.toString();
					}
				}
				posB++;
			}
			posA++;
		}
		return maxString;
	}
}
